package main

import (
	"fmt"
	"sort"
	"strings"
)
var  pl = fmt.Println   // 这样写可以简化打印语句，推荐
/*   判断两个字符串是否互为变位词
lintcode: (158) Two Strings Are Anagrams
Write a method anagram(s,t) to decide if two strings are anagrams or not.

Example
Given s="abcd", t="dcab", return true.

Challenge
O(n) time, O(1) extra space */

//判断切片是否相等
func isSameSlice(s1 []string,s2 []string)  bool{
	if len(s1)==0 && len(s2)==0{
		return true
	} 
	if len(s1)!= len(s2){
		return  false
	}
	for i := range s1 {
	   if s1[i]!=s2[i] {
		   return  false
	   }
	}
	return true
}

func areAnagrams(str1 string,str2 string) bool {
	s1:= strings.Split(str1,"")
	s2:= strings.Split(str2,"")
	//将切片排序
	sort.Strings(s1)
	sort.Strings(s2)

	if isSameSlice(s1,s2){
		return true
	}
	return false
}

func main()  {
	pl("都为空 ",areAnagrams("",""))
	pl("s1为空 ",areAnagrams("as",""))
	pl("s2为空  ",areAnagrams("","sad"))
	pl("长度不一致 ",areAnagrams("abc","abcd"))
	pl("标准情况 ",areAnagrams("abcd","dcab"))
}